﻿using AiXiu.BLL;
using AiXiu.Common;
using AiXiu.IBLL;
using AiXiu.Model;
using System;
using System.Web.UI;

namespace AiXiu.WebSite
{
    /// <summary>
    /// 登录页
    /// </summary>
    public partial class Login : Page
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // 登录验证
                string userName = txtUserName.Text;
                string password = txtPassword.Text;
                IAccountManager userManager = new AccountManager();
                OperResult<TBUsers> operResult = userManager.LoginByUserName(userName, password);
                if (operResult.StatusCode == StatusCode.Succeed)
                {
                    // 缓存用户资料
                    TBUsers user = operResult.ReadResult();
                    IdentityManager.SaveUser(user);
                    // 脚本跳转
                    string redirectUrl = Request.QueryString["ReturnUrl"];
                    if (string.IsNullOrWhiteSpace(redirectUrl))
                    {
                        redirectUrl = "Default.aspx";
                    }
                    this.Redirect("lgnsuccess", redirectUrl);
                }
                else
                {
                    this.Alert("lgnfail", operResult.Message ?? "登录失败！");
                }
            }
        }
    }
}